<template>
	<view class=" ">
		<view class="page bg-white b_y">
			<view>
				<view class="f-d  bg-white p2 a-c bb">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28  w800">姓名</text>
					</view>
					<input class="f28 in w800" type="text" value="" placeholder="请输入姓名" v-model="name" />
				</view>

				<view class="f-d  bg-white p2 a-c bb">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28 w800">手机</text>
					</view>
					<input class="f28 in w800" type="text" value="" placeholder="请输入手机号码" v-model="tel" />
				</view>
				<view class="f-d  bg-white p2 a-c bb">
					<view class="f-d " style="width: 520rpx;">
						<view class=" c_pl" style="width: 180rpx;">
							<text class="f28 w800">地址</text>
						</view>
						<textarea class="f28 in w800" style="width: 65%;" type="text" auto-height="" placeholder="请在地图上选择位置"
							v-model="shop_addr" />
					</view>
					<view class="">
						<text @click="on_dingwen()" class=" cff  q_y f28 pr2 pl2 zhuti_bg1"
							style="padding-top:5rpx;padding-bottom: 8rpx;">选择位置</text>
					</view>
				</view>
				<view class="f-d  bg-white p2 a-c bb">
					<view class="f28  " style="width: 180rpx;">
						<text class="f28 w800">门牌号</text>
					</view>
					<input class="f28 in w800" type="text" value="" placeholder="楼号,门牌等信息" v-model="addr_details" />
				</view>
				<view class="flex j-b bg-white p2 a-c bb">
					<view class="f40 c_pl" style="width: 180rpx;">
						<text class="f40 w800">设为默认</text>
					</view>
					<view @click="on_select()" class="flex a-c  switch_sub mr2" :class="select==1 ? '' : 'j-f'">
						<view class="switch" :class="select==2?'buttonswitchSelected':'buttonswitch'">
						</view>
					</view>
				</view>
			</view>




			<view class="flex j-c a-c pb3">
				<button class="js-image-a zhuti_bg1" type="default "
					style="margin: 20upx;width: 600rpx;color: #fff;border-radius: 45rpx;"
					@tap="on_post">
					<text class=" p20 f40">保存</text>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'newsPublish',
		components: {},
		data() {
			return {

				idtushow: false,
				isYasuo: true,
				imageUrl: '',
				show: false,
				url: '',
				isdingwei: true,

				list: [],
				cata_name: '请选择',
				name: '',
				tel: '',
				shop_addr: '',
				is_tupian: 0,
				select: 2,
				selectLocatin: {
					location: {}
				},

				rendering: false,
				addr_details: '',
				id: 0

			}
		},


		onLoad(on) {
			this.token = uni.getStorageSync('token');
			var that = this;
			if (on.id > 0) {
				this.id = on.id;

				this.init();
			}

		},
		methods: {
			on_select() {
				var select = 2
				if (this.select == 2) {
					select = 1
				}
				this.select = select;
			},
			on_dingwen() {
				this.token = uni.getStorageSync('token');
				if (!this.token) {
					this.is_log();
					return
				}
				let that = this;
				uni.chooseLocation({
					success: function(res) {

						that.shop_addr = res.address;
						that.shop_lat = res.latitude;
						that.shop_lng = res.longitude;
					}
				});
			},
			confirm(val) {
				this.idtushow = false;
				this.shop_lat = val.location.lat
				this.shop_lng = val.location.lng
				this.shop_addr = val.address
			},
			isPhone() {
				let mPattern = /^1[23456789]\d{9}$/;
				return mPattern.test(this.shop_tel);
			},



			on_post() {
				this.token = uni.getStorageSync('token');
				if (!this.token) {
					this.is_log();
					return
				}
				if (!this.name) {
					uni.showToast({
						title: "请输入姓名",
						icon: 'none'
					})
					return
				}
				if (!this.tel) {
					uni.showToast({
						title: "请输入联系电话",
						icon: 'none'
					})
					return
				}
				if (!this.shop_lng) {
					uni.showToast({
						title: "请选择地址",
						icon: 'none'
					})
					return
				}
				if (!this.addr_details) {
					uni.showToast({
						title: "请输入门牌号",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '正在提交中'
				})

				this.add_shop();

			},
			add_shop() {


				// var shop_img = this.shop_img.replace(this.img_url, "")
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					name: this.name,
					tel: this.tel,
					addr: this.shop_addr,
					addr_details: this.addr_details,
					id: this.id,
					shop_lng: this.shop_lng,
					shop_lat: this.shop_lat,
					select: this.select
				};
				this.Htpp.post('/banmi.php/User/user_add_addr', 'app', {
					token: true,
					datalist: requestParams

				}).then(ret => {
					uni.hideLoading()
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					if (ret.data.status == 200) {
						setTimeout(function() {
							uni.$emit('banmi_addr', {
								title: 1,
							});
							uni.navigateBack()
						}, 500);

						return
					}

				})
			},

			init() {
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					id: this.id,
				};
				this.Htpp.post('/banmi.php/User/user_addr_find', 'app', {
					token: true,
					datalist: requestParams
				}).then(ret => {
					if (ret.data.status == 200) {
						var shop_find = ret.data.data.find;
						this.img_url = ret.data.url
						if (shop_find) {
							that.title = '编辑地址';
							uni.setNavigationBarTitle({
								title: that.title
							})
							that.name = shop_find.name;
							that.shop_addr = shop_find.addr;
							that.tel = shop_find.tel;
							that.addr_details = shop_find.addr_details;
							that.shop_lng = shop_find.lng;
							that.shop_lat = shop_find.lat;
							that.select = shop_find.select;
						}
					}
				})
			},




		}
	}
</script>

<style>
	page {
		background-color: #EFEFF4;
	}
   .p2{
	   padding: 40rpx 20rpx;
   }
	.f28 {
		font-size: 40rpx !important;
	}



	.shuru {
		height: 250rpx;
		line-height: 50upx;
		width: 750rpx;
		border-width: 2rpx !important;
		border-right-style: solid !important;
		border-left-color: #FFFFFF !important;
		border-right-color: #FFFFFF !important;
		border-bottom-color: #D9D9D9 !important;
		border-top-color: #FFFFFF !important;

	}

	.input-view {
		font-size: 28upx;
	}

	.choose-location {
		padding: 30rpx;


	}


	.info-box {
		padding: 30rpx 0;

	}

	.info-box-item {
		padding-bottom: 20rpx;
	}

	.feedback-title {

		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 20upx;
		color: #8f8f94;
		font-size: 28upx;
	}



	.in {
		width: 500rpx;
		height: 50rpx;
	}

	.close-view {
		width: 35rpx;
		height: 35rpx;
		border-radius: 50%;
		background-color: #FF5053;
		color: #FFFFFF;
		position: relative;
		top: -110px;
		right: -85px;
		font-size: 20rpx;
	}
</style>